//
//  SXViewController.m
//  108 - 特殊布局
//
//  Created by 董 尚先 on 15/3/20.
//  Copyright (c) 2015年 shangxianDante. All rights reserved.
//

#import "SXLineViewController.h"
#import "SXLineLayout.h"
#import "SXImageCell.h"

@interface SXLineViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
/** 所有的图片名 */
//@property (nonatomic, strong) NSMutableArray *images;
@property (nonatomic, weak) UICollectionView *collectionView;
@end

@implementation SXLineViewController

static NSString *const ID = @"image";

//- (NSMutableArray *)images
//{
//    if (!_images) {
//        self.images = [[NSMutableArray alloc] init];
//        
//        for (int i = 1; i<=12; i++) {
//            [self.images addObject:[NSString stringWithFormat:@"%d", i]];
//        }
//    }
//    return _images;
//}

//加载数据
-(void)setUpData{
    
    NSString *urlStr =  @"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=3&contentType=album&device=iPad&scale=3&version=4.3.8";
    
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (data != nil) {
            
            NSDictionary * mDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            self.mArr = [NSMutableArray arrayWithCapacity:1];
            self.titleArr = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *dic in mDic[@"focusImages"][@"list"]) {
                NSString *imgStr = dic[@"pic"];
                NSString *titleStr = dic[@"longTitle"];
                
                NSURL *imgUrl = [NSURL URLWithString:imgStr];
                NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
                UIImage *img = [UIImage imageWithData:imgData];
                
                [self.mArr addObject:img];
                [self.titleArr addObject:titleStr];
                [self.collectionView reloadData];
            }
            
        }
        
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpData];
    // 创建布局
    SXLineLayout *layout = [[SXLineLayout alloc] init];
    
    // 创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) collectionViewLayout:layout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerNib:[UINib nibWithNibName:@"SXImageCell" bundle:nil] forCellWithReuseIdentifier:ID];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SXImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.image = self.mArr[indexPath.row];
    return cell;
}

#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    // 删除图片名
//    [self.images removeObjectAtIndex:indexPath.item];
//    
//    // 刷新数据
//    //    [self.collectionView reloadData];
//    
//    // 直接将cell删除
//    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    
    NSLog(@"%@",self.titleArr[indexPath.row]);
    
}


@end
